package com.yang.drawcard.video;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.core.net.RestClient;
import com.core.net.callback.IError;
import com.core.net.callback.IFailure;
import com.core.net.callback.ISuccess;
import com.core.net.callback.OnDownLoadProgress;
import com.core.utils.dbtutil.DbtLog;
import com.yang.drawcard.application.ConstValues;
import com.yang.drawcard.utils.ApkDownloadDialog;


/**
 * VideoService
 */
public class VideoService {

    private final String TAG = "MainService";

    private Context context;
    private Handler handler;

    /**
     *
     */
    public VideoService() {

    }

    public VideoService(Context context, Handler handler) {
        this.context = context;
        this.handler = handler;
    }

    // 获取分类列表
    public void getJsonWorks(String url) {

        RestClient.builder()
                //.url(PropertiesUtil.getProperties("platform_ip"))
                .url(url)
                // .params("", "")
                //.params("data", jsonZip)
                .loader(context)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        if (!"".equals(response)) {
                            Bundle bundle = new Bundle();
                            bundle.putString("formjson", response);
                            bundle.putString("status", ConstValues.SUCCESS);
                            if (handler != null) {
                                Message msg = new Message();
                                msg.what = ConstValues.WAIT0;
                                msg.setData(bundle);
                                handler.sendMessage(msg);
                            }
                        } else {
                            Bundle bundle = new Bundle();
                            bundle.putString("formjson", "数据返回异常");
                            bundle.putString("status", ConstValues.ERROR);
                            if (handler != null) {
                                Message msg = new Message();
                                msg.what = ConstValues.WAIT0;
                                msg.setData(bundle);
                                handler.sendMessage(msg);
                            }
                            DbtLog.logUtils("返回数据异常R", response);
                        }
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg2) {
                        Bundle bundle = new Bundle();
                        bundle.putString("formjson", msg2);
                        bundle.putString("status", ConstValues.ERROR);
                        if (handler != null) {
                            Message msg = new Message();
                            msg.what = ConstValues.WAIT0;
                            msg.setData(bundle);
                            handler.sendMessage(msg);
                        }
                    }
                })
                .failure(new IFailure() {
                    @Override
                    public void onFailure() {
                        Bundle bundle = new Bundle();
                        bundle.putString("formjson", "请检查您的网络");
                        bundle.putString("status", ConstValues.EXCEPTION);
                        if (handler != null) {
                            Message msg = new Message();
                            msg.what = ConstValues.WAIT0;
                            msg.setData(bundle);
                            handler.sendMessage(msg);
                        }
                    }
                })
                .builde()
                .get();
    }

    /**
     * 下载txt
     *
     * @param downloadUrl  下载链接 : http://smp.tsingtao.com.cn/static/ddtracepic/cxyschool/knowledge/逸品纯生产品介绍20210420033745.pdf
     * @param textName     本地文件名称 : 123.txt
     * @param isShowDialog 是否显示进度条 false不显示下载进度,true显示下载进度框
     * @param requestCode  用于Activity界面handle的对应回调接收 比如what1,what2
     */
    public void downCatVideoJsonList(String downloadUrl, String textName, final boolean isShowDialog, final int requestCode) {

        if (isShowDialog) {
            // 展示进度条
            ApkDownloadDialog.showDownloadDialog(context);
        }

        RestClient.builder()
                .url(downloadUrl)// http://192.168.31.128:8080/landking/video/LIST.TXT
                // .params("data", jsonZip)
                // .loader(context)// 滚动条
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        // 关闭进度条
                        ApkDownloadDialog.stopDownloadDialog();
                        Bundle bundle = new Bundle();
                        bundle.putString("formjson", response);
                        bundle.putString("status", ConstValues.SUCCESS);
                        if (handler != null) {
                            Message msg = new Message();
                            msg.what = requestCode;
                            msg.setData(bundle);
                            handler.sendMessage(msg);
                        }
                    }
                })
                .onDownLoadProgress(new OnDownLoadProgress() {
                    @Override
                    public void onProgressUpdate(long fileLength, int downLoadedLength) {
                        // 用handle通知主线程刷新进度, progress: 是1-100的正整数
                        Message updateMsg = Message.obtain();
                        updateMsg.obj = fileLength;
                        updateMsg.arg1 = downLoadedLength;

                        if (isShowDialog) {
                            // 进度条进度
                            ApkDownloadDialog.showDownloading(updateMsg);
                        }


                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        // 关闭进度条
                        ApkDownloadDialog.stopDownloadDialog();

                        Bundle bundle = new Bundle();
                        bundle.putString("formjson", msg);
                        bundle.putString("status", ConstValues.ERROR);
                        if (handler != null) {
                            Message msg2 = new Message();
                            msg2.what = requestCode;
                            msg2.setData(bundle);
                            handler.sendMessage(msg2);
                        }
                    }
                })
                .failure(new IFailure() {
                    @Override
                    public void onFailure() {
                        // 关闭进度条
                        ApkDownloadDialog.stopDownloadDialog();

                        Bundle bundle = new Bundle();
                        bundle.putString("formjson", "请检查您的网络");
                        bundle.putString("status", ConstValues.EXCEPTION);
                        if (handler != null) {
                            Message msg = new Message();
                            msg.what = requestCode;
                            msg.setData(bundle);
                            handler.sendMessage(msg);
                        }
                    }
                })
                .name(textName)
                .dir(ConstValues.LOCALPATH)
                .builde()
                .download();
    }


    // 获取列表
    public void getVideoList(int month) {

        String url = "";
        if (0 == month) {
            url = "https://qndc.wxyass.com/json/videolist.json";
        } else if (1 == month) {
            url = "https://qndc.wxyass.com/json/firstmonth.json";
        } else if (2 == month) {
            url = "https://qndc.wxyass.com/json/secondmonth.json";
        } else if (3 == month) {
            url = "https://qndc.wxyass.com/json/thirdmonth.json";
        } else if (4 == month) {
            url = "https://qndc.wxyass.com/json/fourthmonth.json";
        } else if (5 == month) {
            url = "https://qndc.wxyass.com/json/fifthmonth.json";
        } else if (6 == month) {
            url = "https://qndc.wxyass.com/json/sixthmonth.json";
        } else if (7 == month) {
            url = "https://qndc.wxyass.com/json/seventhmonth.json";
        } else if (8 == month) {
            url = "https://qndc.wxyass.com/json/eighthmonth.json";
        } else if (9 == month) {
            url = "https://qndc.wxyass.com/json/ninthmonth.json";
        } else if (10 == month) {
            url = "https://qndc.wxyass.com/json/tenthmonth.json";
        } else if (11 == month) {
            url = "https://qndc.wxyass.com/json/eleventhmonth.json";
        } else if (12 == month) {
            url = "https://qndc.wxyass.com/json/twelfthmonth.json";
        } else if (1834 == month) {// book
            url = "https://qndc.wxyass.com/json/booklist.json";
        }


        RestClient.builder()
                //.url(PropertiesUtil.getProperties("platform_ip"))
                .url(url)
                // .params("data", jsonZip)
                //.params("data", jsonZip)
                .loader(context)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        if (!"".equals(response)) {
                            // 保存信息
                            // String formjson = resObj.getResBody().getContent();
                            Bundle bundle = new Bundle();
                            bundle.putString("formjson", response);
                            bundle.putString("status", ConstValues.SUCCESS);
                            if (handler != null) {
                                Message msg = new Message();
                                msg.what = ConstValues.WAIT0;
                                msg.setData(bundle);
                                handler.sendMessage(msg);
                            }
                        } else {
                            Bundle bundle = new Bundle();
                            bundle.putString("formjson", "数据返回异常");
                            bundle.putString("status", ConstValues.ERROR);
                            if (handler != null) {
                                Message msg = new Message();
                                msg.what = ConstValues.WAIT0;
                                msg.setData(bundle);
                                handler.sendMessage(msg);
                            }
                            DbtLog.logUtils("返回数据异常R", response);
                        }
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg2) {
                        Bundle bundle = new Bundle();
                        bundle.putString("formjson", msg2);
                        bundle.putString("status", ConstValues.ERROR);
                        if (handler != null) {
                            Message msg = new Message();
                            msg.what = ConstValues.WAIT0;
                            msg.setData(bundle);
                            handler.sendMessage(msg);
                        }
                    }
                })
                .failure(new IFailure() {
                    @Override
                    public void onFailure() {
                        Bundle bundle = new Bundle();
                        bundle.putString("formjson", "请检查您的网络");
                        bundle.putString("status", ConstValues.EXCEPTION);
                        if (handler != null) {
                            Message msg = new Message();
                            msg.what = ConstValues.WAIT0;
                            msg.setData(bundle);
                            handler.sendMessage(msg);
                        }
                    }
                })
                .builde()
                .post();

    }
}
